In [1]:
import graphlab
In [2]:
image_train = graphlab.SFrame('image_train_data/')
In [3]:
image_train.head(4)
Out[3]:
In [4]:
knn_model = graphlab.nearest_neighbors.create(image_train,features=['deep_features'],label='id')
In [7]:
graphlab.canvas.set_target('browser')
cat = image_train[18:19]
cat['image'].show()
In [8]:
knn_model.query(cat)
Out[8]:
In [9]:
def get_images_from_ids(query_result):
return image_train.filter_by(query_result['reference_label'],'id')
In [10]:
cat_neighbors = get_images_from_ids(knn_model.query(cat))
In [11]:
cat_neighbors['image'].show()
In [12]:
car = image_train[8:9]
In [13]:
car['image'].show()
In [14]:
get_images_from_ids(knn_model.query(car))['image'].show()
In [15]:
show_neighbors = lambda i : get_images_from_ids(knn_model.query(image_train[i:i+1]))['image'].show()
In [16]:
show_neighbors(8)
In [18]:
show_neighbors(26)
In [20]:
show_neighbors(122)
In [21]:
show_neighbors(2000)
In [23]:
image_train['label'].sketch_summary()
Out[23]:
In [24]:
image_categs = ['dog','cat','automobile','bird']
In [25]:
dog_df = image_train.filter_by('dog','label')
In [28]:
cat_df = image_train.filter_by('cat','label')
auto_df = image_train.filter_by('automobile','label')
bird_df = image_train.filter_by('bird','label')
In [30]:
dog_model = graphlab.nearest_neighbors.create(dog_df,features=['deep_features'],label='id')
cat_model = graphlab.nearest_neighbors.create(cat_df,features=['deep_features'],label='id')
auto_model = graphlab.nearest_neighbors.create(auto_df,features=['deep_features'],label='id')
bird_model = graphlab.nearest_neighbors.create(bird_df,features=['deep_features'],label='id')
In [31]:
image_test = graphlab.SFrame('image_test_data/')
In [35]:
image_test[0:1]['image'].show()
Out[35]:
In [62]:
cat_model.query(image_test[0:1])
#get_images_from_ids(cat_model.query(image_test[0:1]))['image'].show()
Out[62]:
In [64]:
image_train['id'==16289]['image'].show()
In [43]:
get_images_from_ids(knn_model.query(image_test[0:1]))
Out[43]:
In [45]:
dog_model.query(image_test[0:1])
Out[45]:
In [72]:
get_images_from_ids(dog_model.query(image_test[0:1]))['image'].show()
In [67]:
image_train['id'==16976]['image'].show()
In [40]:
import graphlab.aggregate as agg
cat_model.query(image_test[0:1]).groupby('query_label',operations = {'mean_distance' : agg.MEAN('distance')})
Out[40]:
In [46]:
dog_model.query(image_test[0:1]).groupby('query_label',operations = {'mean_distance' : agg.MEAN('distance')})
Out[46]:
In [47]:
image_test_cat = image_test.filter_by('cat','label')
image_test_dog = image_test.filter_by('dog','label')
image_test_automobile = image_test.filter_by('automobile','label')
image_test_bird = image_test.filter_by('bird','label')
In [52]:
dog_dog_neighbors = dog_model.query(image_test_dog, k=1)
In [48]:
dog_cat_neighbors = cat_model.query(image_test_dog, k=1)
In [49]:
dog_auto_neighbors = auto_model.query(image_test_dog,k=1)
In [50]:
dog_brid_neighbors = bird_model.query(image_test_dog,k=1)
In [ ]:
In [55]:
dog_distances= graphlab.SFrame({'dog_dog':dog_dog_neighbors['distance'],'dog-cat':dog_cat_neighbors['distance'],'dog-automobile':dog_auto_neighbors['distance'],
'dog-bird':dog_brid_neighbors['distance']})
In [56]:
dog_distances
Out[56]:
In [57]:
def is_dog_correct(row):
return (row['dog_dog']<row['dog-cat']) and (row['dog_dog']<row['dog-bird']) and (row['dog_dog']<row['dog-automobile'])
In [59]:
dog_distances.apply(is_dog_correct).sum()
Out[59]:
In [ ]:
cat_cat_neighbors = cat_model.query(image_test_dog, k=1)
dog_cat_neighbors = cat_model.query(image_test_dog, k=1)
dog_auto_neighbors = auto_model.query(image_test_dog,k=1)
dog_brid_neighbors = bird_model.query(image_test_dog,k=1)
dog_distances= graphlab.SFrame({'dog_dog':dog_dog_neighbors['distance'],'dog-cat':dog_cat_neighbors['distance'],'dog-automobile':dog_auto_neighbors['distance'],
'dog-bird':dog_brid_neighbors['distance']})
def is_dog_correct(row):
return (row['dog_dog']<row['dog-cat']) and (row['dog_dog']<row['dog-bird']) and (row['dog_dog']<row['dog-automobile'])
In [ ]:
cat_distances.apply(is_cat_correct).sum()